package com.jww.service.user;

import com.jww.dao.BaseDao;
import com.jww.dao.user.UserDao;
import com.jww.dao.user.UserDaoImpl;
import com.jww.pojo.User;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class UserServiceImpl implements UserService {

    //业务层都会调用dao层，所以我们要引入Dao层
    private UserDao userDao;
    public UserServiceImpl() {
        userDao = new UserDaoImpl();
    }

    // 修改用户信息
    public boolean modify(User user) {
        Connection connection = null;
        boolean flag = false;
        try {
            connection = BaseDao.getConnection();
            if(userDao.modify(connection,user) > 0) {
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }

    // 通过userId查询user
    public User getUserById(String id) {
        User user = null;
        Connection connection = null;
        try{
            connection = BaseDao.getConnection();
            user = userDao.getUserById(connection,id);
        }catch (Exception e) {
            e.printStackTrace();
            user = null;
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return user;
    }

    // 根据userCode查询出User
    public User selectUserCodeExist(String userCode) {
        Connection connection = null;
        User user = null;
        try {
            connection = BaseDao.getConnection();
            user = userDao.getLoginUser(connection, userCode);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return user;
    }


    //添加用户
    @Override
    public boolean add(User user) {
        Connection conn = null;
        boolean flag = false;

        try {
            conn = BaseDao.getConnection();
            conn.setAutoCommit(false);//开启JDBC事务管理
            int updateRows = userDao.add(conn,user);
            conn.commit();//没有错误提交事务
            if (updateRows > 0) {
                flag = true;
                System.out.println("add success");
            }else {
                System.out.println("add failed!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                System.out.println("rollback回滚======================");
                conn.rollback();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
        }finally {
            //在service层进行conn连接关闭
            BaseDao.closeResource(conn,null,null);
        }
        return flag;
    }

    //删除用户
    @Override
    public boolean deleteUserById(int id) {
        Connection conn = null;
        boolean flag = false;
        try {
            conn = BaseDao.getConnection();
            conn.setAutoCommit(false);
            int updateRows = userDao.deleteUserById(conn, id);
            if (updateRows > 0) {
                flag = true;
            }
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
            throw new RuntimeException(e);
        }finally {
            BaseDao.closeResource(conn,null,null);
        }
        return flag;
    }


    //用户登录业务
    @Override
    public User login(String userCode, String password) {
        //数据库连接对象和用户对象
        Connection conn = null;
        User user = null;

        try {
            //数据库连接
            conn = BaseDao.getConnection();
            //通过业务层调用对应的具体的数据库操作
            user = userDao.getLoginUser(conn, userCode);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //关闭资源
            BaseDao.closeResource(conn,null,null);
        }
        // 匹配密码
        if(null != user){
            if(!user.getUserPassword().equals(password))
                user = null;
        }
        //返回用户对象
        return user;
    }

    //用户修改密码
    @Override
    public boolean updatePwd(int id, String pwd) {
        //这里创建Connection对象是方便事务的回滚
        Connection conn = null;
        boolean flag = false;
        try {
            conn = BaseDao.getConnection();
            int updatePwd = userDao.updatePwd(conn, id, pwd);
            if(updatePwd > 0){ //大于0说明修改了行数数据库那么，所以是成功
                flag = true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            BaseDao.closeResource(conn,null,null);
        }
        return flag;
    }

    //查询记录数
    @Override
    public int getUserCount(String username, int userRole) {
        Connection conn = null;
        int count = 0;
        try {
            conn = BaseDao.getConnection();
            count = userDao.getUserCount(conn, username, userRole);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            BaseDao.closeResource(conn,null,null);
        }
        return count;
    }

    //查询用户列表
    @Override
    public List<User> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) {
        Connection conn = null;
        List<User> userList = new ArrayList<User>();
        //----------------------排错代码----------
        System.out.println("queryUserName:"+queryUserName);
        System.out.println("queryUserRole:"+queryUserRole);
        System.out.println("currentPageNo:"+currentPageNo);
        System.out.println("pageSize:"+pageSize);
        //----------------------------------------------
        try {
            conn = BaseDao.getConnection();
            userList = userDao.getUserList(conn,queryUserName,queryUserRole,currentPageNo,pageSize);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            BaseDao.closeResource(conn,null,null);
        }
        return userList;
    }


    //@Test
    //public void test()
    //{
    //    UserServiceImpl userService = new UserServiceImpl();
    //    List<User> userList = userService.getUserList(null, 2, 1, 5);
    //    for (int i = 0; i < userList.size(); i++) {
    //        System.out.println(userList.get(i).getUserCode());
    //    }
    //}
}
